package org.locker.device.controller.web;

import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.locker.common.core.domain.R;
import org.locker.service.IPaymentService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

/**
 * (小程序)支付管理
 * @Description:
 * @Author: lark
 * @Date: 2023/10/8
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/app/payment")
public class PaymentController {

    private final IPaymentService paymentService;

    /**
     * 订单支付
     * @param orderNo
     * @param request
     * @return
     */
    @GetMapping("/{orderNo}")
    public R getDeviceInfo(@PathVariable("orderNo") String orderNo,HttpServletRequest request){
        String ip = request.getRemoteAddr();
        return paymentService.orderPay(orderNo,ip);
    }

    /**
     * 支付回调
     * @param xmlData
     * @return
     */
    @PostMapping("/notify")
    public String parseOrderNotifyResult(@RequestBody String xmlData){
        asyncProcessNotify(xmlData);
        return WxPayNotifyResponse.success("成功");
    }

    @Async
    public void asyncProcessNotify(String xmlData) {
        paymentService.notify(xmlData);
    }





}
